package com.qy139.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qy139.auth.mapper.TabRoleMenuMapper;
import com.qy139.auth.service.ITabRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.entity.TabRoleMenu;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author yuhaochen
 * @since 2021-12-01
 */
@Service
public class TabRoleMenuServiceImpl extends ServiceImpl<TabRoleMenuMapper, TabRoleMenu> implements ITabRoleMenuService {

    @Resource
    private TabRoleMenuMapper roleMenuMapper;

    public Result findMenuIdByRoleId(Integer id) {
        return new Result(roleMenuMapper.findMenuIdByRoleId(id));
    }

    @Transactional
    @Override
    public Result addMenu(Integer id, String menusId) {
        QueryWrapper<TabRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rid",id);
        roleMenuMapper.delete(queryWrapper);
        String[] menuids = menusId.split(",");
        for (String menuid : menuids) {
            TabRoleMenu roleMenu = new TabRoleMenu();
            roleMenu.setRid(id);
            roleMenu.setMenuid(Integer.parseInt(menuid));
            roleMenuMapper.insert(roleMenu);
        }
        return new Result(200,"success","");
    }
}
